package com.bytedance.scenedemo.extreme_case;

import com.bytedance.scene.animation.animatorexecutor.NoAnimationExecutor;
import com.bytedance.scene.group.GroupScene;
import com.bytedance.scene.interfaces.PopOptions;
import com.bytedance.scene.interfaces.PushOptions;
import com.bytedance.scene.ktx.SceneExtensions;
import com.bytedance.scene.navigation.NavigationSceneGetter;
import com.bytedance.scenedemo.ResourceTable;
import com.bytedance.scenedemo.benchmark.BenchmarkSamples;
import com.bytedance.scenedemo.lifecycle.EmptyScene100;
import com.bytedance.scenedemo.util.ResUtil;
import com.bytedance.scenedemo.utility.ColorUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ScrollView;
import ohos.agp.components.Text;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * Created by JiangQi on 9/5/18.
 */
public class Case2Scene extends GroupScene {
    private EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());

    @Override
    public ComponentContainer onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        return (ComponentContainer) inflater.parse(ResourceTable.Layout_case_basic_layout,container,false);
    }

    @Override
    public void onViewCreated(Component layout, Intent intent) {
        super.onViewCreated(layout, intent);
        Text textHint= (Text) layout.findComponentById(ResourceTable.Id_text_case0scene_hint);
        ScrollView svMain = (ScrollView) layout.findComponentById(ResourceTable.Id_sv_push_pop_basic_usage_main);
        textHint.setText(Case2Scene.class.getName());
        int value = 0;
        if(getArguments()!=null){
            value= (int) getArguments().getParam("value");
        }
        svMain.setBackground(ColorUtil.getMaterialColor(value));

        Button btnPushAndPop1 = (Button) layout.findComponentById(ResourceTable.Id_case_push_pop_btn_1);
        btnPushAndPop1.setText(ResUtil.getString(getSceneContext(),ResourceTable.String_case_pop_many_btn_1));
        btnPushAndPop1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                SceneExtensions.requireNavigationScene(Case2Scene.this).push(EmptyScene100.class);
                for (int i=0;i<=99;i++) {
                    SceneExtensions.requireNavigationScene(Case2Scene.this).pop();
                }
            }
        });
        Button btnPushAndMany2 = (Button) layout.findComponentById(ResourceTable.Id_case_push_pop_btn_2);
        btnPushAndMany2.setText(ResUtil.getString(getSceneContext(),ResourceTable.String_case_pop_many_btn_2));
        btnPushAndMany2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                PushOptions options = new PushOptions.Builder()
                        .setRemovePredicate(new PushOptions.SingleTaskPredicate(EmptyScene100.class))
                        .build();
                SceneExtensions.getNavigationScene(Case2Scene.this).push(EmptyScene100.class,null,options);

                NavigationSceneGetter.getNavigationScene(Case2Scene.this).pop(new PopOptions.Builder()
                        .setPopUtilPredicate(new PopOptions.CountUtilPredicate(100)).build());
            }
        });
        Button btnPushAndMany3 = (Button) layout.findComponentById(ResourceTable.Id_case_push_pop_btn_3);
        btnPushAndMany3.setText(ResUtil.getString(getSceneContext(),ResourceTable.String_case_pop_many_btn_3));
        btnPushAndMany3.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showToast(ResUtil.getString(getSceneContext()
                        ,ResourceTable.String_case_pop_many_toast_1));
                eventHandler.postTask(new Runnable() {
                    @Override
                    public void run() {
                        for (int i=0;i<=99;i++) {
                            SceneExtensions.requireNavigationScene(Case2Scene.this).pop();
                        }
                        PushOptions options=new PushOptions.Builder()
                                .setRemovePredicate(new PushOptions.SingleTaskPredicate(EmptyScene100.class))
                                .build();
                        SceneExtensions.getNavigationScene(Case2Scene.this).push(EmptyScene100.class,null,options);
                        showToast(ResUtil.getString(getSceneContext()
                                ,ResourceTable.String_case_pop_many_toast_2));
                    }
                },3000);
            }
        });
    }

    @Override
    public void onActivityCreated(Intent intent) {
        super.onActivityCreated(intent);
    }
}
